/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package toesp.bean;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import toesp.entity.Groupmenu;

/**
 *
 * @author satria.sp
 */
public class GroupmenuBean {
    private EntityManagerFactory emf;

    public GroupmenuBean() {
        emf = Persistence.createEntityManagerFactory("swing-researchPU");
    }

    private EntityManager getEntityManager() {
        return emf.createEntityManager();
    }
    
    private void getCloseEntityManager() {
        emf.close();
    }
    
    public List<Groupmenu> getAllByGroup(int p_intGroupId) throws Exception { 
        List<Groupmenu> lstGroup = null;  
        try {
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Groupmenu o WHERE o.groupId=:p_intGroupId ORDER BY o.menuId ASC");    
            q.setParameter("p_intGroupId", p_intGroupId);
            lstGroup = q.getResultList();
        } catch (Exception e) {        
            System.out.println("Salahmu : "+e.getMessage());
        }finally{
            //this.getCloseEntityManager();
        }  
        
        return lstGroup;
    }   
    
    public Groupmenu getOnebyId(int p_intmenuId) throws Exception {
        Groupmenu o = null;
        try {              
            EntityManager em = getEntityManager();
            Query q = em.createQuery("SELECT o FROM Groupmenu o WHERE o.menuId=:p_intmenuId");   
            q.setParameter("p_intmenuId", p_intmenuId);            
            List<Groupmenu> lstPg = q.getResultList();
            o = (Groupmenu) lstPg.get(0);
        } catch (Exception e) {            
        }finally{   
            //this.getCloseEntityManager();
        }  
        return o;
    }
    
    public void commitaj(){
        
    }
}
